package com.anosine.gpsl.android.ui.fragment;

import com.anosine.gpsl.android.R;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.*;
import android.widget.TextView;

/**
 * @Title: ShowConfirmDlg
 * @Package com.anosine.gpsl.android.ui.fragment
 * @Description: 
 * @author: sunny
 * @date: 2015-11-24
 * @version V1.0
 */
public class ShowConfirmDlg extends DialogFragment implements View.OnClickListener, DialogInterface.OnKeyListener {
    private TextView mTvShowInfo1;
    private TextView tv_show_info_dlg_title;
    private static View.OnClickListener sOnClickOkListener;
    private static String sInfo,sTitle;
    private static boolean sIsOk;
    /**
     * 方法名：showDlg(FragmentManager manager, String s)
     * 功能：显示dialog
     * 参数：FragmentManager manager 上下文， String s 显示的内容
     * @return DialogFragment
     * */
    public static DialogFragment showDlg(FragmentManager manager, String s,String t) {
        return showDlg(manager, null, s,t);
    }
    /**
     * 方法名：showDlg(FragmentManager manager, View.OnClickListener listener, String s)
     * 功能：显示dialog
     * 参数：FragmentManager manager 上下文，listener 点击事件， String s 显示的内容
     * @return DialogFragment
     * */
    public static DialogFragment showDlg(FragmentManager manager, View.OnClickListener listener, String s,String t) {
        return showDlg(manager, listener, s, t,false);
    }
    /**
     * 方法名：showDlg(FragmentManager manager, View.OnClickListener listener, String s, boolean isJustShowOk)
     * 功能：创建ShowConfirmDlg并显示
     * 参数：FragmentManager manager 上下文，listener 点击事件， String s 显示的内容,isJustShowOk 是否只有确定没有取消按钮
     * @return DialogFragment
     * */
    public static DialogFragment showDlg(FragmentManager manager, View.OnClickListener listener, String s,String t, boolean isJustShowOk) {
        sOnClickOkListener = listener;
        sInfo = s;
        sTitle=t;
        sIsOk=isJustShowOk;
        ShowConfirmDlg dlg = new ShowConfirmDlg();
        dlg.show(manager, null);
        return dlg;
    }
    /**
     * 方法名：onCreate(Bundle savedInstanceState)
     * 功能：初始化
     * 参数：Bundle savedInstanceState Activity结束前，如果需要保存状态，
     * 就在onsaveInstanceState中，将状态数据以key-value的形式放入到savedInstanceState中。
     * 这样，当一个Activity被创建时，就能从onCreate的参数savedInsanceState中获得状态数据
     * @return none
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.custom_dlg);
    }
    /**
     * 方法名：onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     * 功能：重写onCreateView决定DialogFragment的布局，创建该DialogFragment对应的视图，你必须在这里创建自己的视图并返回给调用者
     * 参数：LayoutInflater inflater 动态载入的界面, ViewGroup container , Bundle savedInstanceState
     * @return View 创建的视图
     * */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dlg_show_confirm, container);
        mTvShowInfo1 = (TextView) view.findViewById(R.id.tv_show_info_dlg_confirm);
        mTvShowInfo1.setText(sInfo);
        tv_show_info_dlg_title=(TextView)view.findViewById(R.id.tv_show_info_dlg_title);
        tv_show_info_dlg_title.setText(sTitle);
        view.findViewById(R.id.btn_confirm_dlg_cancel).setOnClickListener(this);
        view.findViewById(R.id.btn_confirm_dlg_ok).setOnClickListener(this);
        if (sIsOk){
            view.findViewById(R.id.btn_confirm_dlg_cancel).setVisibility(View.GONE);
        }

        return view;
    }
    /**
     * 方法名：onActivityCreated(Bundle arg0)
     * 功能：当activity的onCreated()方法返回后调用此方法
     * 参数：Bundle arg0
     * @return none
     * */
    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        int mScreenWidth = wm.getDefaultDisplay().getWidth();
        layoutParams.width = (int) (0.9f * mScreenWidth);
        getDialog().setCanceledOnTouchOutside(false);
        if (sIsOk){
            getDialog().setOnKeyListener(this);
        }
    }
    /**
     * 方法名：onClick(View v)
     * 功能：点击事件
     * 参数：View v 被点击的view
     * @return none
     * */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm_dlg_ok:
                if (sOnClickOkListener != null) {
                    sOnClickOkListener.onClick(v);
                }
                dismiss();
                break;
			case R.id.btn_confirm_dlg_cancel:
				dismiss();
				break;
        }
    }
    /**
     * 方法名：onCancel(DialogInterface dialog)
     * 功能：使对话框消除
     * 参数：DialogInterface dialog DialogInterface接口声明的两个方 法，cancel()和dismiss()使对话框取或者消除
     * @return 无
     * */
    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        dismissSetInfoNull();
    }
    /**
     * 方法名：onDismiss(DialogInterface dialog)
     * 功能：使对话框消除
     * 参数：DialogInterface dialog DialogInterface接口声明的两个方 法，cancel()和dismiss()使对话框取或者消除
     * @return 无
     * */
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        dismissSetInfoNull();
    }
    /**
     * 方法名：dismissSetInfoNull()
     * 功能：确认点击事件设置成null
     * 参数：无
     * @return 无
     * */
    private void dismissSetInfoNull() {
        sOnClickOkListener = null;
    }
    /**
     * 方法名：onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent)
     * 功能：当一个关键事件被发送到这个对话框时，调用一个回调函数的接口定义。该回调将在该对话框中的关键事件被调用之前调用。
     * @param DialogInterface dialogInterface The dialog the key has been dispatched to
     * @param int keyCodeThe code for the physical key that was pressed
     * @param KeyEvent keyEvent The KeyEvent object containing full information aboutthe event
     * @return boolean
     * */
    @Override
    public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return false;
    }
}
